<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>东财机构买卖</title>
		<link href="css/text.css" rel="stylesheet" type="text/css" />
		<style type="text/css">							 
			.fixedBox::-webkit-scrollbar {
				width: 12px;     
				height: 12px;				
			}					  
			#towrite {margin-top:-10px}	
			
			table{
			width:100px; 
			border-collapse:collapse; 
			text-align:center;
			white-space: nowrap;
			}
			td,th{border:1px solid #696969}					
		</style>
	</head>
	<body>
		<div id="towrite" class="fixedBox">			
		</div>		
		<script type="text/javascript" src="js/json2html.js"></script>
		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script type="text/javascript" src="DatePicker/WdatePicker.js"> </script> 
		<script>
			var g = 0;  //不要1板改为0  ,要点“清除缓存”
			var bkcolor = $("body").css("backgroundColor");
			var day = new Date();
			var tt = "";			
			window.onload = function() {			
				mymonth = day.getMonth()+1;
				myday = day.getDate();
				if(mymonth >= 10){mymonth = mymonth;}else{mymonth = "0" + mymonth;}
				if(myday >= 10){myday = myday;}else{myday = "0" + myday;}
				tt = day.getFullYear() + "-" + mymonth + "-" + myday;
				changeStyle();
				getSource(0);				
			}
											
			function changeStyle() {
				var obj	 = document.getElementById("towrite");	
				obj.style.left= "0px"; 				
				obj.style.width= "100%";
				//obj.style.height= "620px";						
				obj.style.fontSize="12px";
			}
			
			Date.prototype.Format = function (fmt) {
				var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
				};
				if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
				return fmt;
			};
			
			function UnUnicode(str) {
				return unescape(str.replace(/\\/g, "%"));
			}			
						
			var trid = "";
			function changeCss(obj,hid,id){						
				var tlist = document.getElementById(id).getElementsByTagName("tr");					
				for(var i = 0; i < tlist.length; i++){														
					tlist[i].style.backgroundColor = bkcolor; 					
				}
				trid = hid;
				var tr = document.getElementById(hid);				
				tr.style.backgroundColor = "#483D8B";						
			}
			
			function hover(){  //鼠标经过
				var rows = document.getElementsByTagName('tr');
				for(var i=0;i<rows.length;i++){
					rows[i].onmouseover=function(){						
						if(this.id != ""){  //过滤表头
							this.style.background="#800000";
						}
					}
					rows[i].onmouseout=function(){
						if(this.id != ""){  //过滤表头
							if(this.id != trid){  //过滤选中行
								this.style.background=bkcolor;
							}else{
								this.style.background="#483D8B";
							}
						}						
					}					
				}
			}			
			
			
			function keepTwoDecimal(num) {  
				   var result = parseFloat(num);  
				   if (isNaN(result)) {  
					   //alert('传递参数错误，请检查！');  
					   return false;  
				   }  
				   result = Math.round(num * 100) / 100;  
				   return result;  
			};
			
			function NumberTransform(num) { 
				var result = num;
				if(Math.abs(num) > 100000000){
					result = num / 100000000;
					result = result.toFixed(2) + "亿";
				}else if(Math.abs(num) > 10000){
					result = num/10000;
					result = result.toFixed(2) + "万";
				}				   
				return result;  
			};
			
			var st = "";
			var et = "";
			function updaterq(p) {
				//alert(p);
				var day = new Date();				
				day.setTime(day.getTime());
				var x = 0;
				var t  = "";
				if(localStorage.getItem('dcjyrq') != null) {  //判断有无缓存
					t = localStorage.getItem('dcjyrq'); //取缓存
				}
				//alert(t)
				var tc = "";
				for (i = 0; i < 100; i++){						
					if(i > 0){
						day.setTime(day.getTime() - 24*60*60*1000);
					}										
					tt = day.Format("yyyy-MM-dd");
					//alert(tt)
					if(t.indexOf(tt) > -1){
						if(x == 0){
							et = tt;
						}
						x++;
						if(x == p){
							st = tt;
							break;
						}					
					}else{
						var week = day.getDay();
						if(week != 0 && week != 6){
						//alert(tt+"|"+week)
							var url = "https://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery112309109506934287157_1668573968459&sortColumns=NET_BUY_AMT,TRADE_DATE,SECURITY_CODE&sortTypes=-1,-1,1&pageSize=50&pageNumber=1&reportName=RPT_ORGANIZATION_TRADE_DETAILSNEW&columns=ALL&source=WEB&client=WEB&filter=(TRADE_DATE>='" + tt + "')(TRADE_DATE<='" + tt + "') ";
							var res = "";
							$.ajax({
								type: "get",
								url: url,
								dataType: "text",
								async : false,
								cache:false,
								timeout:3000,					
								success: function(data) {									
									res = data;
								}
							});
							if(res.indexOf("返回数据为空") == -1){
								tc += tt + ",";
								if(x == 0){
									et = tt;
								}
								x++;
								//localStorage.setItem('dcjyrq', t + "," + tt);  //交易日期写入缓存
								if(x == p){
									st = tt;
									break;
								}	
							}
						}						
					}
				}
				//alert(tc)
				if(tc != ""){
					localStorage.setItem('dcjyrq', t + "," + tc);  //交易日期写入缓存
				}				
				//alert("et:"+et)
				//alert("st:"+st)  				
			}						
			
			var x = "";
			var y = 0;
			var gl = "";  //高亮
			function getSource(p) {				
				var n = 0;
				if(p == 0){
					n = 1;
				}else if(p == 1){
					n = 3;
				}else if(p == 2){
					n = 5;
				}else if(p == 3){
					n = 10;
				}else if(p == 4){
					n = 30;
				}
				updaterq(n);
				
				var p1 = "<a href='javascript:void(0)' id='gpx0' onclick='getSource(0)'>" + et + "日</a>";	
				var p2 = "<a href='javascript:void(0)' id='gpx1' onclick='getSource(1)'>" + "3日" + "</a>";
				var p3 = "<a href='javascript:void(0)' id='gpx2' onclick='getSource(2)'>" + "5日" + "</a>";
				var p4 = "<a href='javascript:void(0)' id='gpx3' onclick='getSource(3)'>" + "10日" + "</a>";					
				var p5 = "<a href='javascript:void(0)' id='gpx4' onclick='getSource(4)'>" + "30日" + "</a>";
				
				var qh = "<a style='position:absolute; left:500px;font-size:12px;' href='javascript:void(0)' onclick='clearstorage()'>" + "清除缓存" + "</a>";
				
				var div = document.getElementById('towrite');				
				div.innerHTML = '<font size="3">东财机构买卖</font><font size="2">（' + p1 + p2 + p3 + p4 + p5 + '）</font>' + qh;	
				var aObj = document.getElementById('gpx' + p);				
				aObj.style.color = "#FFA07A"; 
				
				if(p == 0){
					st = et;
				}
				
				url = "https://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery112309109506934287157_1668573968459&sortColumns=NET_BUY_AMT,TRADE_DATE,SECURITY_CODE&sortTypes=-1,-1,1&pageSize=5000&pageNumber=1&reportName=RPT_ORGANIZATION_TRADE_DETAILSNEW&columns=ALL&source=WEB&client=WEB&filter=(TRADE_MARKET_CODE in (\"069001001001\",\"069001001006\",\"069001001003\",\"069001002001\",\"069001002002\",\"069001002005\"))(TRADE_DATE>='" + st + "')" ; 					
				var res = [];
				$.ajax({
					type: "get",
					url: url,
					dataType: "text",
					async : false,
					cache:false,
					timeout:3000,					
					success: function(data) {
						var ss = data.replace(/ /g,"")
						ss = ss.match(/,"data":(\S*)}],/)[1];
						var arr =  JSON.parse(ss+"}]");
						for (var i = 0; i < arr.length; i++){
							res.push({"龙虎榜":arr[i]["SECURITY_CODE"],"SECURITY_CODE":arr[i]["SECURITY_CODE"],"SECURITY_NAME_ABBR":arr[i]["SECURITY_NAME_ABBR"],"TRADE_DATE":arr[i]["TRADE_DATE"].replace("00:00:00",""),
							"CLOSE_PRICE":arr[i]["CLOSE_PRICE"],"CHANGE_RATE":keepTwoDecimal(arr[i]["CHANGE_RATE"]),"NET_BUY_AMT":NumberTransform(arr[i]["NET_BUY_AMT"]),"EXPLANATION":arr[i]["EXPLANATION"]});
						}		
					}
				});
				//document.write(JSON.stringify(res));
				var json = res;
				
				//创建table					
				var table=document.createElement("table");										
				var thead=document.createElement("thead");
				table.appendChild(thead);					
				var tr=document.createElement("tr");
				thead.appendChild(tr);					
				
				var m = 0;
				var v = 0;
				var b = 0;
				var gl = "机构净额";  //高亮
				for(var key in json[0]){  //表头
					key = key.replace('CLOSE_PRICE','现价').replace('NET_BUY_AMT','机构净额').replace('CHANGE_RATE','涨幅%').replace('EXPLANATION','上榜原因');
					key = key.replace('TRADE_DATE','上榜日期').replace('SECURITY_CODE','代码').replace('SECURITY_NAME_ABBR','名称');
					var th=document.createElement("th");						
					th.innerHTML=key;						
					tr.appendChild(th);
					if(key == '龙虎榜'){
						th.style.color = "#00FFFF";
					}
					if(gl == key.replace("%","")){
						v = m;
						th.style.color = "#FF6633";	
					}
					if(key == "上榜原因"){
						b = m;
						y = m;
						th.style.color = "#FFA500";	
					}
					m++;
				}
									
				var tbody=document.createElement("tbody");					
				table.appendChild(tbody);
				
				//遍历json
				for(var i = 0; i < json.length; i++){					
					var tr=document.createElement("tr");						
					var p = 1;
					var dm = "";
					var mc = "";
					for(var key in json[i]){ //行元素遍历						
						var td=document.createElement("td");							
						var rc = json[i][key];
						if(p == 1){  //代码列
							dm = rc;
							tr.id = i + dm;
							td.innerHTML = "<a href=http://www.treeid/code_" + dm + " id='" + p + "' style='color:#00FFFF' onclick='lhb();changeCss(this,\"" + i + dm + "\",\"gg\")'>榜单</a>";
						}else{
							td.innerHTML = "<a href=http://www.treeid/code_" + dm + " id='" + p + "' onclick='changeCss(this,\"" + i + dm + "\",\"gg\")'>" + rc + "</a>";
						}
					
						if(p == v + 1){							
							td.getElementsByTagName("a")[0].style.color = "#FF6633";
						}
						if(p == b + 1){																	
							td.getElementsByTagName("a")[0].style.color = "#FFA500";
						}							
						tr.appendChild(td);
						p++;
					}						
					tbody.appendChild(tr);
				}					
				var div = document.getElementById("towrite");									
				div.appendChild(table);
				table.id = "gg";
				table.style.marginTop = "10px";					
				//changeTable(table,"7");
				makeSortable(table);
				hover();
			}			
			
			//隐藏某列				
            function changeTable(table,ss) {	
				var trs = table.rows;
				ss = ss + '#';
				var aa = ss.split('#');
				for (var j = 0; j < aa.length; j++) {
					if(aa[j] != ""){
						for (var i = 0, len = trs.length; i < len; i++) {					
							var p = aa[j];
							var cell = trs[i].cells[p]; 
							cell.style.display = 'none';
						}
					}
				}					
			}
			
			function lhb() {
				var url = "http://www.treeid/dlghttp://page.tdx.com.cn:7615/site/kggx/tk_ygzl_lhb.html?info=xxxxxx&amp;pageId=dpyd&amp;color=0&amp;bkcolor=000000";
				window.open(url, "_self");
			}
			
			function makeSortable(table) {			
				var headers=table.getElementsByTagName("th");
				for(var i=0;i<headers.length;i++){				
					(function(n){
						var flag=false;
							
						headers[n].onclick=function(){
													
							showCol(table,n);		//高亮
							var tbody=table.tBodies[0];
							var rows=tbody.getElementsByTagName("tr");
							rows=Array.prototype.slice.call(rows,0);

							//基于第n个<td>元素的值对行排序
							rows.sort(function(row1,row2){
								headtxt = headers[n].textContent;								
								var cell1=row1.getElementsByTagName("td")[n];//获得第n个单元格
								var cell2=row2.getElementsByTagName("td")[n];																
								var val1=cell1.textContent||cell1.innerText;//获得文本内容
								var val2=cell2.textContent||cell2.innerText;
								if(headtxt.indexOf("名称") != -1 || headtxt.indexOf("上榜") != -1){
									val1 = val1;
									val2 = val2;
								}else{											
									if(val1.indexOf("万") != -1){
										val1 = parseFloat(val1)*10000;
									}else if(val1.indexOf("亿") != -1){
										val1 = parseFloat(val1)*100000000;
									}else{
										val1 = parseFloat(val1);
									}
									
									if(val2.indexOf("万") != -1){
										val2 = parseFloat(val2)*10000;
									}else if(val2.indexOf("亿") != -1){
										val2 = parseFloat(val2)*100000000;
									}else{
										val2 = parseFloat(val2);
									}
								}
								if(val1<val2){
									return -1;
								}else if(val1>val2){
									return 1;
								}else{
									return 0;
								}
							});
							if(flag){
								rows.reverse();
							}
							
							for(var i=0;i<rows.length;i++){
								tbody.appendChild(rows[i]);
							}

							flag=!flag;
						}
					}(i));
				}
			}
			
			function showCol(table,iCol){			
				var oTable = table;
				var id = oTable.id;
				for (i=0;i < oTable.rows.length; i++){										
					for (j=0;j < oTable.rows[0].cells.length; j++){	
						oTable.rows[i].cells[0].style.color = "#00FFFF";
						oTable.rows[i].cells[j].style.color = "#E5E5E5";						
						oTable.rows[i].cells[y].style.color = "#FFA500";										
					}
					oTable.rows[i].cells[iCol].style.color = "#FF6633";					
				}
				
				var otagsA = oTable.getElementsByTagName("a");  //a变色
				for (i = 0; i < otagsA.length; i++) {
					otagsA[i].style.color = "#E5E5E5";
					if(otagsA[i].id == 1){
						otagsA[i].style.color = "#00FFFF";								
					}
					if(otagsA[i].id == y + 1){
						otagsA[i].style.color = "#FFA500";								
					}
					if(otagsA[i].id == iCol + 1){
						otagsA[i].style.color = "#FF6633";
					}
				}				
			}	

			function clearstorage(){				
				if(localStorage.getItem('dcjyrq') != null){						
					localStorage.removeItem('dcjyrq'); //清除缓存localStorage
				}				
				alert('清除完成');
			}
		</script>	
	</body>
</html>